//
//  CXPlayView.m
//  XiMaFM
//
//  Created by hcios on 2017/9/9.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "CXPlayView.h"

#define kAngleToRadian(x) (x / 180.0 * M_PI)

@interface CXPlayView()

@end

@implementation CXPlayView

-(instancetype)init {
    if (self = [super init]) {
        UIImageView * backgroundView = [[UIImageView alloc] initWithImage:kGetImage(@"tabbar_np_normal")];
        [self addSubview:backgroundView];
        
        [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        _circleImageView = [[UIImageView alloc] initWithImage:kGetImage(@"tabbar_np_loop")];
        [backgroundView addSubview:_circleImageView];
        [_circleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(2);
            make.right.mas_equalTo(-2);
            make.bottom.mas_equalTo(-8);
        }];
        backgroundView.userInteractionEnabled = YES;
        _circleImageView.userInteractionEnabled = YES;
        [self.playButton setImage:kGetImage(@"tabbar_np_play") forState:UIControlStateNormal];
    }
    return self;
}


#pragma mark - 懒加载
-(UIButton *)playButton{
    if (!_playButton) {
        _playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        //取消高亮
        [_playButton setHighlighted:NO];
        
        [_playButton setBackgroundImage:kGetImage(@"avatar_bg") forState:UIControlStateSelected];
        [_playButton setImage:kGetImage(@"toolbar_pause_h_p") forState:UIControlStateSelected];
        [self addSubview:_playButton];
        [_playButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(2);
            make.right.mas_equalTo(-2);
            make.bottom.mas_equalTo(-7);
        }];
        
        [_playButton bk_addEventHandler:^(UIButton * sender) {
            
            if ([self.delegate respondsToSelector:@selector(playButtonDidClick:)]) {
                sender.selected = !sender.selected;
                self.link.paused = !sender.selected;
                [self.delegate playButtonDidClick:sender.selected];
            }
            
        } forControlEvents:UIControlEventTouchUpInside];
    }
    return _playButton;
}


-(void)rotation{
    self.circleImageView.layer.transform = CATransform3DRotate(self.circleImageView.layer.transform,kAngleToRadian(72/60.0) , 0, 0, 1);
    
}

-(UIImageView *)contentImageview{
    if (!_contentImageview) {
        //申明内容视图
        _contentImageview = [[UIImageView alloc] init];
        [_circleImageView addSubview:_contentImageview];
        [_contentImageview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(8, 8, 8, 8));
        }];
        
        [_contentImageview bk_addObserverForKeyPath:@"image" options:NSKeyValueObservingOptionNew task:^(id obj, NSDictionary *change) {
            //开启定时器
            self.link.paused = NO;
            self.playButton.selected = YES;
        }];
        
        _contentImageview .layer.cornerRadius = 24;
        _contentImageview.clipsToBounds = YES;
    }
    return _contentImageview;
}

-(CADisplayLink *)link{
    if (!_link) {
        _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(rotation)];
        
        [_link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    }
    return _link;
}


@end
